package com.lottery.system.service.activitystatus.operater.impl;

import com.lottery.system.common.errorcode.ServiceErrorCodeConstants;
import com.lottery.system.common.exception.ServiceException;
import com.lottery.system.dao.dataobject.ActivityUserDO;
import com.lottery.system.dao.mapper.ActivityUserMapper;
import com.lottery.system.service.activitystatus.enums.SequenceEnum;
import com.lottery.system.service.activitystatus.operater.ActivityStatusAbstractOperator;
import com.lottery.system.service.dto.ConvertActivityDTO;
import com.lottery.system.service.enums.ActivityUserStatusEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author: xiaoxie
 * create: 2024-09-12 20:08
 * @BelongsProject: lottery-system
 * @BelongsPackage: com.lottery.system.service.activitystatus.operater.impl
 * description: 中奖人员扭转状态策略实现类
 */
@Component
public class UserStatusOperatorImpl implements ActivityStatusAbstractOperator {
    private final static Logger logger = LoggerFactory.getLogger(ActivityStatusOperatorImpl.class);
    @Autowired
    private ActivityUserMapper activityUserMapper;
    /**
     * @description: 中奖人员扭转状态责任链排序的策略
     * @author: xiaoxie
     * @date: 2024/9/12 20:52
     * @param: []
     * @return: com.lottery.system.service.activitystatus.enums.SequenceEnum
     **/
    @Override
    public SequenceEnum sequence() {
        return SequenceEnum.BEFORE;
    }

    /**
     * @description: 中奖人员是否需要扭转状态的策略
     * @author: xiaoxie
     * @date: 2024/9/12 20:12
     * @param: [convertActivityDTO]
     * @return: java.lang.Boolean
     **/
    @Override
    public Boolean needConvert(ConvertActivityDTO convertActivityDTO) {
        if(null == convertActivityDTO) {
            logger.warn(ServiceErrorCodeConstants.ACTIVITY_STATUS_ERROR.getMsg());
            throw new ServiceException(ServiceErrorCodeConstants.ACTIVITY_STATUS_ERROR);
        }
        //判断用户是否存在
        List<ActivityUserDO> activityUserDOList
                = activityUserMapper.selectActivityUserListByIds(convertActivityDTO.getActivityId(), convertActivityDTO.getUserIds());
        if(null == activityUserDOList || activityUserDOList.isEmpty()) {
            logger.warn(ServiceErrorCodeConstants.USER_DATA_EMPTY.getMsg());
            throw new ServiceException(ServiceErrorCodeConstants.USER_DATA_EMPTY);
        }
        //判断用户状态是否为COMPLETED
        for(ActivityUserDO activityUserDO : activityUserDOList) {
            if(activityUserDO.getStatus().equalsIgnoreCase(ActivityUserStatusEnum.COMPLETED.name())) {
                return false;
            }
        }
       return true;
    }

    /**
     * @description: 中奖人员是否成功扭转状态的策略
     * @author: xiaoxie
     * @date: 2024/9/12 20:12
     * @param: [convertActivityDTO]
     * @return: java.lang.Boolean
     **/
    @Override
    public Boolean convertStatus(ConvertActivityDTO convertDTO) {
        if(null == convertDTO) {
            logger.warn(ServiceErrorCodeConstants.ACTIVITY_STATUS_ERROR.getMsg());
            throw new ServiceException(ServiceErrorCodeConstants.ACTIVITY_STATUS_ERROR);
        }
        try {
            activityUserMapper.updateStatusByActivityIdAndUserIds(convertDTO.getActivityId()
                                                                  ,convertDTO.getUserIds()
                                                                  ,convertDTO.getActivityUserStatusTarget().name());
            return true;
        }catch (Exception e) {
            logger.warn("用户状态扭转失败: ",e);
            return false;
        }
    }
}
